<html>
    <head>
        <title>
            Hoang Hong Hai
        </title>

        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    </head>




    <body>

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <?php
        require_once 'model/db.php';
        ?>

        <h1>Hoang Hong Hai</h1>
        <?php
        $query = "SELECT * FROM companies";
        $companies = getPages($query);
        ?>
        <?php
        $query = "SELECT * FROM apps";
        $apps = getPages($query);
        ?>
        <form method="POST">
            Min_fans<input type="text" name="min_fans" id="min_fans" ><br>
            Max_fans<input type="text" name="max_fans" id="max_fans" ><br>
            Keyword <input type="text" name="keyword" value="You"><br>
            Company<select name="id_company">
                <option value="0"><?php echo "----" ?></option>
                <?php foreach ($companies as $company): ?>
                    <option value="<?php echo $company['id'] ?>"><?php echo $company['name'] ?></option>
                <?php endforeach; ?>
            </select>
            Apps<select name="id_app">
                <option value="0"><?php echo "----" ?></option>
                <?php foreach ($apps as $app): ?>
                    <option value="<?php echo $app['id'] ?>"><?php echo $app['name'] ?></option>
                <?php endforeach; ?>
            </select>
            <input type="submit" name="submit" value ="Search">
        </form>

        <?php if (isset($_POST['submit'])): ?>
            <?php
            $min_fans = $_POST['min_fans'];
            $max_fans = $_POST['max_fans'];
            $keyword = $_POST['keyword'];

            if (empty($keyword)) {
                if (empty($max_fans) && empty($min_fans)) {
                    $query = "SELECT * FROM pages 
                  LIMIT 0, 10
                  ";
                    $pages = getPages($query);
                } elseif (empty($max_fans)) {
                    $query = "SELECT * FROM pages 
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
                  WHERE
              
                 (likes >= $min_fans)
                  LIMIT 0, 10
                  ";
                    $pages = getPages($query);
                } elseif (empty($min_fans)) {
                    $query = "SELECT * FROM pages 
                INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
                  WHERE 
               
                     (likes <= $max_fans)
                  LIMIT 0, 10
                  ";
                    $pages = getPages($query);
                } else {
                    $query = "SELECT * FROM pages 
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
                  WHERE
                 
                      (likes <= $max_fans)
                  AND  (likes >= $min_fans)
                  LIMIT 0, 10
                  ";
                    $pages = getPages($query);
                }
            } else {
                if (empty($max_fans) && empty($min_fans)) {
                    $query = "SELECT * FROM pages 
                   INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                   INNER JOIN apps ON apps.id=apps_pages.id_app
                   INNER JOIN companies ON companies.id=apps.id_company
                   WHERE
                  
                  (description LIKE '% $keyword %')
                  LIMIT 0, 10
                  ";
                    $pages = getPages($query);
                } elseif (empty($max_fans)) {
                    $query = "SELECT * FROM pages 
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company                
                  WHERE
                
                  (description LIKE '% $keyword %')
                  AND  (likes >= $min_fans)
                  LIMIT 0, 10
                  ";
                    $pages = getPages($query);
                } elseif (empty($min_fans)) {
                    $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company 
                   WHERE
              
                  (description LIKE '% $keyword %')
                  AND  (likes <= $max_fans)
                  LIMIT 0, 10
                  ";
                    $pages = getPages($query);
                } else {
                    $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
                      WHERE 
                    
                     (description LIKE '% $keyword %') 
                      AND(likes >= $min_fans)
                      AND (likes <= $max_fans)
                  LIMIT 0, 10
                  ";
                    $pages = getPages($query);
                }
            }
            var_dump($query);
            ?>

            <?php if (!empty($pages)): ?>
                <table border="1">
                    <tr style="background: #ccff66">
                        <td> id </td>
                        <td> picture </td>
                        <td> likes </td>
                        <td> description</td>
                        <td>name</td>

                    </tr>
                    <?php foreach ($pages as $page): ?>
                        <tr>
                            <td><?php echo $page['id'] ?></td>
                            <td><img src= "<?php echo $page['picture'] ?>"></td>
                            <td><?php echo $page['likes'] ?></td>
                            <td><?php echo substr($page['description'], 0, 100) ?></td>
                            <td><?php echo $page['name'] ?></td>

                            <?php // var_dump($page);die(); ?>
                        </tr>
                    <?php endforeach; ?>
                </table> 
            <?php endif; ?>
        <?php endif; ?>
        <script>
            $(document).ready(function(){
                $("#min_fans").click(function() {
                    var min_fans = $("#min_fans").val();

                    if ($.isNumeric(min_fans)) {
                        alert('is Number');
                    } else {
                        alert('is not number');
                    }
                });
            });
            $(document).ready(function(){
           $("#max_fans").click(function() {
                    var max_fans = $("#max_fans").val();

                    if ($.isNumeric(max_fans)) {
                        alert('is Number');
                    } else {
                        alert('is not number');
                    }
                });
            });

        </script>

    </body>

</html>